﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using S7.Windows.Menu;

namespace S7.Windows.Orders
{
    /// <summary>
    /// Interaction logic for AddAnOrder.xaml
    /// </summary>
    public partial class AddAnOrder : Window
    {
        public AddAnOrder()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            //lbSelectedItem.Items
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
            {
                try
                {
                    Menus.lstMenus = Menus.Search().ToList();
                    lbCategories.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        try
                        {
                            lbCategories.Items.Add("All");
                            foreach (var item in Menus.lstMenus)
                            {
                                lbCategories.Items.Add(item.MenuName);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }), null);
                    List<MenuItem> lstItems = MenuItem.Search();
                    MenuItem.lstItems = new List<MenuItem>();
                    foreach (var item in lstItems)
                    {
                        MenuItem.lstItems.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void lbMenuItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            lbSelectedItem.Items.Add(lbMenuItems.SelectedItem);
        }

        private void lbCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedMenu = lbCategories.SelectedItem.ToString();
            Thread thr = new Thread(new ThreadStart(() =>
            {
                try
                {
                    while (true)
                    {
                        if (MenuItem.lstItems != null)
                        {
                            if (selectedMenu == "All")
                            {
                                lbMenuItems.Dispatcher.Invoke(new ThreadStart(() =>
                                {
                                    try
                                    {
                                        lbMenuItems.ItemsSource = MenuItem.lstItems;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                    }
                                }), null);
                                break;
                            }
                            else
                            {
                                var query = from menu in Menus.lstMenus
                                            where menu.MenuName == selectedMenu
                                            select menu;
                                List<Menus> SelectedMenu = query.ToList();
                                int currentMenuID = SelectedMenu[0].MenuID;

                                var items = from item in MenuItem.lstItems
                                            where item.MenuID == currentMenuID
                                            select item;
                                lbMenuItems.Dispatcher.Invoke(new ThreadStart(() =>
                                {
                                    try
                                    {
                                        lbMenuItems.ItemsSource = items;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                    }
                                }), null);
                                break;
                            }
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
            thr.IsBackground = true;
            thr.Start();
        }
    }
}
